package worktable

import (
	"time"

	"gitee.com/xuesongtao/collect/app/dao"
	pslog "gitee.com/xuesongtao/ps-log"
	"github.com/gogf/gf/frame/g"
)

var (
	IsTest bool // 是否为测试环境

	plg *pslog.PsLog
)

// ================================初始化对应的方法放在下面, 调用放到 init_linux.go ===============================

func init() {
	InitPlg()
}

func InitPlg() {
	var err error
	plg, err = pslog.NewPsLog(pslog.WithAsync2Tos(), pslog.WithCleanUpTime(12*time.Hour), pslog.WithTaskPoolSize(20))
	if err != nil {
		panic(err)
	}
	if err := plg.TailLogs(); err != nil {
		panic(err)
	}
	if err := plg.Register(GetPlgDefaultHandler()); err != nil {
		panic(err)
	}

	AddLogPath()
	// 执行一次
	plg.CronLogs()
}

// GetPlgDefaultHandler 获取默认 handler
func GetPlgDefaultHandler() *pslog.Handler {
	excludeErrMsgs := g.Cfg().GetStrings("pusherrlog.exclude_contents")
	return &pslog.Handler{
		Change:    -1,             // 每次都持久化 offset
		Tail:      true,           // 实时监听
		ExpireDur: 25 * time.Hour, // 因为日志默认按天分割
		Targets: []*pslog.Target{
			{
				Content:  "[ERRO]", // xxxx-xx-xx.log 中查找满足 [ERRO]
				Excludes: excludeErrMsgs,
				To:       []pslog.PsLogWriter{&dao.ToDingDing{}},
			},
			{
				Content:  "500 ", // error-xxxxxxxx.log 中查找满足 "500 "
				Excludes: excludeErrMsgs,
				To:       []pslog.PsLogWriter{&dao.ToDingDing{}},
			},
		},
	}
}
